import { request } from './request.js'

/**
 * 获取所有票务数据
 * @returns {Promise<Array>} 票务数据数组
 */
export const getAllTickets = async () => {
  try {
    const response = await request.get('/api/events')
    return response.data
  } catch (error) {
    console.error('获取票务数据失败:', error)
    // 返回模拟数据作为备选
    return [
      {
        id: 1,
        name: '周杰伦演唱会',
        price: 380.00,
        stock: 100,
        status: 'available',
        description: '2024周杰伦嘉年华世界巡回演唱会门票，包含经典曲目演唱，豪华舞台效果，互动环节等精彩内容。演出时长约150分钟，提前1小时入场。',
        createdAt: '2024-01-15 10:30:00',
        poster: 'https://picsum.photos/seed/concert1/300/400',
        category: 'concert',
        startTime: '2024-03-15 19:30',
        endTime: '2024-03-15 22:00',
        artists: ['周杰伦']
      },
      {
        id: 2,
        name: 'NBA季后赛门票',
        price: 880.00,
        stock: 50,
        status: 'available',
        description: '2024 NBA季后赛门票，湖人VS勇士巅峰对决，感受顶级篮球赛事的魅力。场馆提供餐饮服务，禁止携带专业相机入场。',
        createdAt: '2024-01-16 14:20:00',
        poster: 'https://picsum.photos/seed/basketball1/300/400',
        category: 'sports',
        startTime: '2024-04-10 10:00',
        endTime: '2024-04-10 12:30',
        artists: ['勒布朗·詹姆斯', '库里']
      },
      {
        id: 3,
        name: '复仇者联盟5首映',
        price: 120.00,
        stock: 0,
        status: 'soldOut',
        description: '复仇者联盟5：终局之战首映IMAX电影票，漫威宇宙最新力作，震撼视觉效果。IMAX厅提供最佳观影体验，提前30分钟入场。',
        createdAt: '2024-01-17 09:15:00',
        poster: 'https://picsum.photos/seed/movie1/300/400',
        category: 'movie',
        startTime: '2024-05-20 00:00',
        endTime: '2024-05-20 02:30',
        artists: ['罗伯特·唐尼', '克里斯·埃文斯']
      },
      {
        id: 4,
        name: '2024上海国际音乐节',
        price: 580.00,
        stock: 80,
        status: 'available',
        description: '2024上海国际音乐节，汇聚全球顶级交响乐团，演奏贝多芬、莫扎特等古典音乐大师作品。音乐厅提供高端音响设备，建议正装出席。',
        createdAt: '2024-01-18 16:45:00',
        poster: 'https://picsum.photos/seed/music1/300/400',
        category: 'music',
        startTime: '2024-06-05 19:00',
        endTime: '2024-06-05 21:30',
        artists: ['上海交响乐团']
      }
    ]
  }
}

/**
 * 添加新票务
 * @param {Object} ticketData - 票务数据
 * @returns {Promise<Object>} 添加结果
 */
export const addTicket = async (ticketData) => {
  try {
    const response = await request.post('/api/events', ticketData)
    return response.data
  } catch (error) {
    console.error('添加票务失败:', error)
    throw error
  }
}

/**
 * 更新票务信息
 * @param {number} id - 票务ID
 * @param {Object} ticketData - 票务数据
 * @returns {Promise<Object>} 更新结果
 */
export const updateTicket = async (id, ticketData) => {
  try {
    const response = await request.put(`/api/events/${id}`, ticketData)
    return response.data
  } catch (error) {
    console.error('更新票务失败:', error)
    throw error
  }
}

/**
 * 删除票务
 * @param {number} id - 票务ID
 * @returns {Promise<Object>} 删除结果
 */
export const deleteTicket = async (id) => {
  try {
    const response = await request.delete(`/api/events/${id}`)
    return response.data
  } catch (error) {
    console.error('删除票务失败:', error)
    throw error
  }
}